package com.echo.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.echo.activity.R;
import com.echo.util.ScreenUtils;

/**
 * Created by EchoGe on 2016/6/8.
 */
public class EditDialog{

    private final ThreadLocal<Context> mContext = new ThreadLocal<>();

    private TextView mTitle;
    private LinearLayout mLine1,mLine2,mLine3;
    private TextView mText1,mText2,mText3,mNegativeButton,mPositiveButton;;
    private EditText mEdit1,mEdit2,mEdit3;

    private Dialog mDialog;

    public EditDialog(Context context) {
        this.mContext.set(context);
        builder();
    }

    public EditDialog builder() {
        View view = LayoutInflater.from(mContext.get()).inflate(
                R.layout.dialog_edit, null);
        mTitle= (TextView) view.findViewById(R.id.dialog_title);
        mLine1= (LinearLayout) view.findViewById(R.id.line1_layout);
        mLine2= (LinearLayout) view.findViewById(R.id.line2_layout);
        mLine3= (LinearLayout) view.findViewById(R.id.line3_layout);
        mText1= (TextView) view.findViewById(R.id.line_text1);
        mText2= (TextView) view.findViewById(R.id.line_text2);
        mText3= (TextView) view.findViewById(R.id.line_text3);
        mNegativeButton= (TextView) view.findViewById(R.id.left_button);
        mPositiveButton= (TextView) view.findViewById(R.id.right_button);
        mEdit1= (EditText) view.findViewById(R.id.line1_edit);
        mEdit2= (EditText) view.findViewById(R.id.line2_edit);
        mEdit3= (EditText) view.findViewById(R.id.line3_edit);

        mDialog = new Dialog(mContext.get(), R.style.EditDialogStyle);
        mDialog.setContentView(view);

        view.setLayoutParams(new FrameLayout.LayoutParams((int) (ScreenUtils.getScreenWidth(mContext.get()) * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));

        return this;
    }

    public EditDialog setTitle(String text){
        mTitle.setText(text);
        return this;
    }

    public EditDialog setLine1(String text,String hint,boolean password){
        mLine1.setVisibility(View.VISIBLE);
        mText1.setText(text);
        mEdit1.setHint(hint);
        if(password)
            mEdit1.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
        return this;
    }

    public EditDialog setLine2(String text,String hint,boolean password){
        mLine2.setVisibility(View.VISIBLE);
        mText2.setText(text);
        mEdit2.setHint(hint);
        if(password)
            mEdit2.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
        return this;
    }

    public EditDialog setLine3(String text,String hint,boolean password){
        mLine3.setVisibility(View.VISIBLE);
        mText3.setText(text);
        mEdit3.setHint(hint);

        if(password)
            mEdit3.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
        return this;
    }

    public void setText1(String text){this.mText1.setText(text);}

    public void setText2(String text){this.mText2.setText(text);}

    public void setText3(String text){this.mText3.setText(text);}

    public String getText1(){
        return mEdit1.getText().toString();
    }

    public String getText2(){
        return mEdit2.getText().toString();
    }

    public String getText3(){
        return mEdit3.getText().toString();
    }

    public EditDialog setPositiveButton(String text,
                                        final View.OnClickListener listener) {
        if ("".equals(text)) {
            mPositiveButton.setText(R.string.confirm);
        } else {
            mPositiveButton.setText(text);
        }
        mPositiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }
                mDialog.dismiss();
            }
        });
        return this;
    }

    public EditDialog setNegativeButton(String text,
                                        final View.OnClickListener listener) {
        if ("".equals(text)) {
            mNegativeButton.setText(R.string.cancel);
        } else {
            mNegativeButton.setText(text);
        }
        mNegativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(v);
                }
                mDialog.dismiss();
            }
        });
        return this;
    }

    public void show() {
        mDialog.show();
    }

}

